package com.concise.appapi.service;

import com.concise.appapi.service.model.RecommendTheArticleModel;
import com.concise.appapi.service.model.TheArticleListsModel;
import com.initialize.pub.dao.ArticleDAO;
import com.initialize.pub.generate.dao.ArticleMapper;
import com.initialize.pub.generate.pojo.Article;
import com.initialize.pub.pojo.Result;
import com.initialize.pub.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: concise-blog
 * @description:
 * @author: Mr.Jane
 * @create: 2019-10-13 00:20
 **/
@Slf4j
@Service
public class ArticleService {

    final
    ArticleDAO articleDAO;

    public ArticleService(ArticleDAO articleDAO, ArticleMapper articleMapper) {
        this.articleDAO = articleDAO;
        this.articleMapper = articleMapper;
    }

    public List<RecommendTheArticleModel> getRecommendedArticles() {
        List<Article> recommendedArticles = articleDAO.getRecommendedArticles();
        return ArticleToModel(recommendedArticles);
    }

    private List<RecommendTheArticleModel> ArticleToModel(List<Article> articleList) {
        List<RecommendTheArticleModel> articleModelList = new ArrayList<>();
        for (Article article : articleList) {
            RecommendTheArticleModel recommendTheArticleModel = new RecommendTheArticleModel();
            recommendTheArticleModel.setAid(article.getAid());
            recommendTheArticleModel.setTheTitle(article.getTitle());
            articleModelList.add(recommendTheArticleModel);
        }
        return articleModelList;
    }

    public List<TheArticleListsModel> getAListOfArticles() {
        List<Article> recommendedArticles = articleDAO.getAListOfArticles();
        return articleToArticleListModel(recommendedArticles);
    }

    public List<TheArticleListsModel> articleToArticleListModel(List<Article> recommendedArticles) {
        List<TheArticleListsModel> theArticleListsModelList = new ArrayList<>();
        for (Article recommendedArticle : recommendedArticles) {
            TheArticleListsModel theArticleListsModel = TheArticleListsModel.builder().build();
            theArticleListsModel.setAid(recommendedArticle.getAid());
            String articleContent = recommendedArticle.getContent();
            articleContent = ToolUtil.removeMarkdown(articleContent);
            int length = articleContent.length();
            log.info("length-->{}", length);
            if (length > 100) {
                articleContent = articleContent.substring(0, 100);
            }
            articleContent += "...";
            theArticleListsModel.setBriefContent(articleContent);
            theArticleListsModel.setThumbUpFor(recommendedArticle.getThumbUpNumber());
            theArticleListsModel.setUpdateTime(recommendedArticle.getUpdateTime());
            theArticleListsModel.setTheTitle(recommendedArticle.getTitle());
            theArticleListsModelList.add(theArticleListsModel);
        }
        return theArticleListsModelList;
    }

    final
    ArticleMapper articleMapper;

    public Result getArticleDetailsBasedOnId(int id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        if (Objects.isNull(article)) {
            return Result.error("你发现了一个新大陆呢");
        }
        return Result.success(article);
    }


    public Result getAllTheCategories() {

        return null;
    }

    public List<TheArticleListsModel> getAListOfArticlesUnderCategories(int cid) {
        List<Article> recommendedArticles = articleDAO.selByLabel(cid);
        return articleToArticleListModel(recommendedArticles);
    }
}
